import requests
import json


def test_active_interface(encrypted_message):
    """
    测试激活接口的POST请求

    参数:
        encrypted_message: 加密的消息字符串
    """
    # 请求地址
    url = "http://192.168.170.33:6000/api/online/active"

    # 请求头，指定内容类型为JSON
    headers = {"Content-Type": "application/json"}

    # 请求体
    payload = {"encryptedMessage": encrypted_message}

    try:
        # 发送POST请求
        response = requests.post(url=url, headers=headers, data=json.dumps(payload))

        # 打印响应状态码
        print(f"Response status code: {response.status_code}")

        # 解析JSON响应
        response_data = response.json()
        print("Full response content:")
        print(json.dumps(response_data, indent=2, ensure_ascii=False))

        # 检查响应格式和内容
        if response_data.get("code") == "200" and response_data.get("success") is True:
            print("\nActivation successful!")
            if "data" in response_data and "encryptedMessage" in response_data["data"]:
                print(
                    f"Returned encrypted message: {response_data['data']['encryptedMessage']}"
                )
        else:
            print(
                f"\nActivation failed: {response_data.get('message', 'Unknown error')}"
            )

    except requests.exceptions.RequestException as e:
        print(f"Request error occurred: {str(e)}")
    except json.JSONDecodeError:
        print("Response content is not valid JSON format")
    except Exception as e:
        print(f"Error occurred while processing response: {str(e)}")


if __name__ == "__main__":
    # 测试用的加密消息，可以根据实际情况修改
    test_encrypted_message = "test_encrypted_message_123456"

    print(
        f"Testing activation interface with encrypted message: {test_encrypted_message}"
    )

    test_active_interface(test_encrypted_message)
